package com.liuzhongni.wahahanews.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.liuzhongni.wahahanews.R;
import com.liuzhongni.wahahanews.headlineclass.special.Normal;
import com.liuzhongni.wahahanews.singleton.VolleySingleton;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by dllo on 16/1/13.
 */
public class DetailsActivity extends AppCompatActivity implements View.OnClickListener {
    private Normal normalContent = new Normal();
    private TextView textViewTitle;
    private TextView textViewSource;
    private TextView textViewTime;
    private TextView textViewBody;
    private ImageView imageViewToTop;
    private Button shareBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        json();
    }

    private void json() {
        final Intent intent = getIntent();
        String url = "http://c.3g.163.com/nc/article/"+intent.getStringExtra("docid")+"/full.html";
        Log.d("DetailsActivity", url);
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Log.d("DetailsActivity", response);
                response = response.replaceFirst(intent.getStringExtra("docid"), "normal");
                normalContent = gson.fromJson(response, Normal.class);
                init();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("DetailsActivity", "失败");
            }
        });
        VolleySingleton.getOurInstance().addRequest(request);

    }

    private void init() {
//        imageViewToTop = (ImageView) findViewById(R.id.details_iv);
        textViewTitle = (TextView) findViewById(R.id.details_title);
        textViewSource = (TextView) findViewById(R.id.details_source);
        textViewTime = (TextView) findViewById(R.id.details_time);
        textViewBody = (TextView) findViewById(R.id.details_body);
        shareBtn = (Button) findViewById(R.id.share_btn);
        textViewTitle.setText(normalContent.getNormal().getTitle());
        textViewSource.setText(normalContent.getNormal().getSource());
        textViewTime.setText(normalContent.getNormal().getPtime());

        String body = normalContent.getNormal().getBody();
        textViewBody.setText(Html.fromHtml(body));
        shareBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
//        listView.setSelection(0);
        showShare();

    }
    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(getString(R.string.ssdk_oks_share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(this);
    }
}
